<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item class="typeClass" label="类别" prop="categoryId">
        <treeselect style="height: 32px!important" v-model="queryParams.categoryId" :options="categoryList" :normalizer="normalizer" placeholder="请选择类别" />
      </el-form-item>
      <el-form-item label="商品名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品级别" prop="shopLeval">
        <el-input
          v-model="queryParams.shopLeval"
          placeholder="请输入商品级别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="shopPrice">
        <el-input
          v-model="queryParams.shopPrice"
          placeholder="请输入商品价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品简介" prop="shopIntroduce">
        <el-input
          v-model="queryParams.shopIntroduce"
          placeholder="请输入商品简介"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['book:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['book:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['book:shop:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['book:shop:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="商品ID" align="center" prop="shopId" />-->
      <el-table-column label="类别ID" align="center" prop="categoryId" />
      <el-table-column label="商品名称" align="center" prop="shopName" />
      <el-table-column label="商品级别" align="center" prop="shopLeval" />
      <el-table-column label="商品价格" align="center" prop="shopPrice" />
      <el-table-column label="商品图片" align="center" prop="shopImg" >
        <template slot-scope="scope">
          <img :src="getImgUrl(scope.row.shopImg)" width="40" height="40" class="head_pic"/>
        </template>
      </el-table-column>
      <el-table-column label="商品简介" align="center" prop="shopIntroduce" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['book:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['book:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别" prop="categoryId">
          <treeselect v-model="form.categoryId" :options="categoryList" :normalizer="normalizer" placeholder="请选择类别" />
<!--          <el-input v-model="form.categoryId" placeholder="请输入类别ID" />-->
        </el-form-item>
        <el-form-item label="商品名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品级别" prop="shopLeval">
          <el-input v-model="form.shopLeval" placeholder="请输入商品级别" />
        </el-form-item>
        <el-form-item label="商品价格" prop="shopPrice">
          <el-input v-model="form.shopPrice" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="商品图片" prop="shopImg">
          <image-upload ref="imageUpload" style="margin-top: 130px" @input="getImgData"></image-upload>
        </el-form-item>
        <el-form-item label="商品简介" prop="shopIntroduce">
          <el-input v-model="form.shopIntroduce" placeholder="请输入商品简介" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listShop, getShop, delShop, addShop, updateShop, exportShop } from "@/api/book/shop";
import {listCategory} from "@/api/book/category";
import ImageUpload from "../../../components/ImageUpload/index";
import API_CONFIG from '../../../../vue.config'
export default {
  name: "Shop",
  components: {
    ImageUpload,
    Treeselect
  },
  data() {
    return {
      fileList:[],
      // imge地址记录
      imageUrl: '',
      //分类列表
      categoryList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品管理表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        shopName: null,
        shopLeval: null,
        shopPrice: null,
        shopImg: null,
        shopIntroduce: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCategoryList()
  },
  methods: {
    /**获取imgURL地址*/
    getImgUrl(data){
      return API_CONFIG.devServer.proxy[process.env.VUE_APP_BASE_API].target + data
    },
    /** 转换图书分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.categoryName,
        children: node.children
      };
    },
    /**获取imge信息*/
    getImgData(data){
      this.form.shopImg = data
      this.imageUrl = data
      console.log('data:',data)
    },
    /** 查询图书分类列表 */
    getCategoryList() {
      this.loading = true;
      listCategory().then(response => {
        this.categoryList = [];
        const data = { categoryId: 0, categoryName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "categoryId", "categoryFatherid");
        this.categoryList.push(data);
      });
    },
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      setTimeout(()=>{
        this.$refs.imageUpload.update()
      },100)

      this.form = {
        shopId: null,
        categoryId: null,
        shopName: null,
        shopLeval: null,
        shopPrice: null,
        shopImg: null,
        shopIntroduce: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shopId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopId = row.shopId || this.ids
      getShop(shopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品管理";
        console.log(this.$refs)
        setTimeout(()=>{
          this.$refs.imageUpload.getValue(response.data.shopImg)
          console.log(this.$refs.imageUpload.getValue())
        },100)

        // this.$refs['imageUpload'].value = response.data.shopImg
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shopId != null) {
            updateShop(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopIds = row.shopId || this.ids;
      this.$confirm('是否确认删除商品管理编号为"' + shopIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delShop(shopIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportShop(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style lang="scss">
.typeClass .el-form-item__content{
  width: 192px;
  height: 32px;
}
.vue-treeselect{
  height: 32px!important;
}
.vue-treeselect .vue-treeselect__control{
  height: 32px !important;
}
.vue-treeselect__placeholder{
  line-height: 32px;
}
.el-select{
  width: 100%;
}
.component-upload-image {
  margin-top: 0px!important;
}
.dialog-footer {
  text-align: center;
}
</style>
